﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ft_module_parser.pdaconversion
{
    class divamodgen
    {
        public List<divamods> Divamods = new List<divamods>();

        public void SetU1P2P(string pvid, string unlock1p, string unlock2p, string song_name)
        {
            var d = this.Divamods.Where(c => c.pvid.ToString().Contains(pvid));
            foreach (var i in d)
            {
                i.unlock1p = unlock1p;
                i.unlock2p = unlock2p;
                i.song_name = song_name;
            }
        }

        public void GenerateDivamodsAFT(string path)
        {

        }

        public void GenerateDivamods(string path)
        {
            if (!Directory.Exists(path + "\\divamods"))
                Directory.CreateDirectory(path + "\\divamods");

            foreach (var i in Divamods)
            {
                //if (Directory.Exists(path + "\\divamods\\pv_" + i.pvid))
                //    Directory.Delete(path + "\\divamods\\pv_" + i.pvid, true);
                if (!Directory.Exists(path + "\\divamods\\pv_" + i.pvid))
                    Directory.CreateDirectory(path + "\\divamods\\pv_" + i.pvid);
                string modpath = path + "\\divamods\\pv_" + i.pvid + "\\";

                StreamWriter outp = new StreamWriter(modpath + "mod.txt");
                outp.WriteLine("# GENERATED AUTOMATICALLY DO NOT EDIT");
                outp.WriteLine("name=pv_" + string.Format("{0:000}", i.pvid));
                outp.WriteLine("type=pv");
                outp.WriteLine("author=Sega");
                outp.WriteLine("pvid=" + i.pvid);
                outp.WriteLine("ver=2");
                outp.Close();

                outp = new StreamWriter(modpath + "pv_db.txt");
                outp.WriteLine("pv_000.bpm=160");
                outp.WriteLine("pv_000.date=20190501");

                if (i.easy)
                {
                    outp.WriteLine("pv_000.difficulty.easy.level=2");
                    outp.WriteLine("pv_000.difficulty.easy.script_file_name=rom/script/pv_" + string.Format("{0:000}", i.pvid) + "_easy.dsc");
                    outp.WriteLine("pv_000.difficulty.easy.script_format=0x11062018");
                    outp.WriteLine("pv_000.difficulty.easy.version=1");
                }

                if (i.extreme)
                {
                    outp.WriteLine("pv_000.difficulty.extreme.level=7");
                    outp.WriteLine("pv_000.difficulty.extreme.script_file_name=rom/script/pv_" + string.Format("{0:000}", i.pvid) + "_extreme.dsc");
                    outp.WriteLine("pv_000.difficulty.extreme.script_format=0x11062018  ");
                    outp.WriteLine("pv_000.difficulty.extreme.version=1");
                }

                if (i.hard)
                {
                    outp.WriteLine("pv_000.difficulty.hard.level=5");
                    outp.WriteLine("pv_000.difficulty.hard.script_file_name=rom/script/pv_" + string.Format("{0:000}", i.pvid) + "_hard.dsc");
                    outp.WriteLine("pv_000.difficulty.hard.script_format=0x11062018");
                    outp.WriteLine("pv_000.difficulty.hard.version=1");
                }

                if (i.normal)
                {
                    outp.WriteLine("pv_000.difficulty.normal.level=5");
                    outp.WriteLine("pv_000.difficulty.normal.script_file_name=rom/script/pv_" + string.Format("{0:000}", i.pvid) + "_normal.dsc");
                    outp.WriteLine("pv_000.difficulty.normal.script_format=0x11062018");
                    outp.WriteLine("pv_000.difficulty.normal.version=1");
                }

                outp.WriteLine("pv_000.disp2d.set_name=440");
                if (i.duet)
                {
                    outp.WriteLine("pv_000.duet.num=2");
                    outp.WriteLine("pv_000.duet.type=USER_SETTING");
                }
                outp.WriteLine("pv_000.lyric.01=");
                if (i.pvid < 800)
                    outp.WriteLine("pv_000.motion.01=PV" + string.Format("{0:000}", (i.pvid + 100)) + "_P1");
                else outp.WriteLine("pv_000.motion.01=PV" + string.Format("{0:000}", i.pvid) + "_P1");
                if (i.duet)
                {
                    if (i.pvid < 800)
                        outp.WriteLine("pv_000.motion2P.01=PV" + string.Format("{0:000}", (i.pvid + 100)) + "_P2");
                    else outp.WriteLine("pv_000.motion2P.01=PV" + string.Format("{0:000}", i.pvid) + "_P2");
                }
                int counter = 0;
                foreach (var c in i.item_pv)
                {
                    outp.WriteLine("pv_000.pv_item." + string.Format("{0:00}", counter) + "=" + c);
                    counter++;
                }

                outp.WriteLine("pv_000.sabi.play_time=25.0");
                outp.WriteLine("pv_000.sabi.start_time=25.0");
                outp.WriteLine("pv_000.se_name=42_button10");
                outp.WriteLine("pv_000.song_file_name=rom/sound/song/pv_" + string.Format("{0:000}", i.pvid) + ".ogg");
                if (i.song_name != "")
                {
                    if (i.pvid < 800)
                        outp.WriteLine("pv_000.song_name=" + i.song_name + " (Concert)");
                    else outp.WriteLine("pv_000.song_name=" + i.song_name);
                }
                else
                    outp.WriteLine("pv_000.song_name=PV_" + string.Format("{0:000}", i.pvid));
                outp.WriteLine("pv_000.song_name_reading=PV_" + string.Format("{0:000}", i.pvid));
                outp.WriteLine("pv_000.songinfo.arranger=unk");
                outp.WriteLine("pv_000.songinfo.illustrator=unk");
                outp.WriteLine("pv_000.songinfo.lyrics=unk");
                outp.WriteLine("pv_000.songinfo.music=unk");
                outp.WriteLine("pv_000.songinfo.pv_editor=unk");
                outp.WriteLine("pv_000.title_image.aet_name=title_eff");
                outp.WriteLine("pv_000.title_image.time=0");
                outp.WriteLine("pv_000.unlock1P=" + i.unlock1p);
                if (i.duet)
                {
                    outp.WriteLine("pv_000.unlock2P=" + i.unlock2p);
                }
                outp.Close();

                outp = new StreamWriter(modpath + "pv_field.txt");
                int fieldcounter = 0;
                for (int field = 1; field < 5; field++)
                {
                    //0 = A
                    //1 = B
                    //2 = C
                    //3 = FIN

                    counter = 0;
                    //heh fix later
                    var lista3da = i.a3da.Distinct().ToList();

                    foreach (var c in lista3da.ToList())
                    {
                        if (field == 0)
                        {
                            if (((c.Contains("LOOP"))))
                                lista3da.Remove(c);
                            if (((!c.Contains("EFF"))))
                                lista3da.Remove(c);
                        }

                        if (field == 1)
                        {
                            if (((!c.EndsWith("_A")))
                                && (c.Contains("LOOP")))
                                lista3da.Remove(c);

                        }

                        if (field == 2)
                        {
                            if (((!c.EndsWith("_B")))
                                && (c.Contains("LOOP")))
                                lista3da.Remove(c);
                            
                        }


                        if (field == 3)
                        {
                            if (((!c.EndsWith("_C")))
                                && (c.Contains("LOOP")))
                                lista3da.Remove(c);
                            
                        }

                        if (field == 4)
                        {
                            if (lista3da.Any(d => d.Contains("_FIN")))
                            {
                                if (((!c.EndsWith("_FIN")))
                                    && (c.Contains("LOOP")))
                                    lista3da.Remove(c);
                                
                                
                            } else
                            {
                                if (((!c.EndsWith("_C")))
                                && (c.Contains("LOOP")))
                                    lista3da.Remove(c);
                                
                            }
                        } else
                        {
                            if (((c.Contains("_FIN"))))
                                lista3da.Remove(c);
                        }

                    }

                    foreach (var c in lista3da)
                    {
                        if (c.Contains("EFF"))
                        {
                            outp.WriteLine("pv_000.field." + string.Format("{0:00}", field) + ".auth_3d_frame_list." + counter + "=-1");
                        }
                        else {
                            if (field == 1)
                            outp.WriteLine("pv_000.field." + string.Format("{0:00}", field) + ".auth_3d_frame_list." + counter + "=0");
                            else outp.WriteLine("pv_000.field." + string.Format("{0:00}", field) + ".auth_3d_frame_list." + counter + "=-1");

                        };
                        counter++;
                    }
                    outp.WriteLine("pv_000.field." + string.Format("{0:00}", field) + ".auth_3d_frame_list.length=" + counter);

                    counter = 0;
                    foreach (var c in lista3da)
                    {
                        outp.WriteLine("pv_000.field." + string.Format("{0:00}", field) + ".auth_3d_list." + counter + "=" + c);
                        counter++;
                    }
                    outp.WriteLine("pv_000.field." + string.Format("{0:00}", field) + ".auth_3d_list.length=" + counter);
                    
                    if (i.pvid < 800)
                        outp.WriteLine("pv_000.field." + string.Format("{0:00}", field) + ".stage=STGPV" + string.Format("{0:000}", (i.pvid + 100)));
                    else outp.WriteLine("pv_000.field." + string.Format("{0:00}", field) + ".stage=STGPV" + string.Format("{0:000}", i.pvid));
                }

                outp.WriteLine("pv_000.field.length=4");
                outp.Close();

            }
        }
    }
}
